package com.earl.eatbao.service.impl;

import com.earl.eatbao.commons.dao.BaseRepsitoty;
import com.earl.eatbao.commons.service.BaseServiceImpl;
import com.earl.eatbao.dao.eatbao.GoodsDao;
import com.earl.eatbao.dao.eatbao.OrderDetailDao;
import com.earl.eatbao.dao.eatbao.OrdersDao;
import com.earl.eatbao.domain.eatbao.Goods;
import com.earl.eatbao.domain.eatbao.OrderDetail;
import com.earl.eatbao.domain.eatbao.Orders;
import com.earl.eatbao.exception.DomainSecurityException;
import com.earl.eatbao.service.OrderDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @version:1.0
 * @author: MissYou
 * @date : 14:56
 * @desc:
 */
@Service
public class OrderDetailServiceImpl extends BaseServiceImpl<OrderDetail, Long> implements OrderDetailService {

    // desc:日记工具logger
    private static final Logger logger = LoggerFactory.getLogger(OrderDetailServiceImpl.class);

    @Autowired
    private OrderDetailDao orderDetailDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private OrdersDao ordersDao;

    @Override
    protected BaseRepsitoty<OrderDetail, Long> getDao() {
        return this.orderDetailDao;
    }


    @Override
    public Boolean addOrderDetailFromGoods(Long goodsId, Long orderId, Integer number) {

        logger.info("<====== 进入addOrderDetailFromGoods:方法,{[goodsId, orderId] :"
                + goodsId + ":" + orderId + " } ====");

        Boolean result = false;
        Goods goods = goodsDao.findOne(goodsId);
        Orders orders = ordersDao.findOne(orderId);
        if ((goods.getNumber() - number >= 0) && number > 0) {
            if (null != goods && null != orders) {
                OrderDetail orderDetail = goodsToOrderDetail(goods, number);
                orderDetail.setOrderId(Math.toIntExact(orderId));
                orderDetail.setCreatedBy(orders.getCreatedBy());
                orderDetail.setLastModifiedBy(orders.getLastModifiedBy());
                orderDetail.setCreatedDate(new Date());
                orderDetail.setLastModifiedDate(new Date());
                orderDetailDao.save(orderDetail);

                //添加订单的总计价格
                orders.setNumber(orders.getNumber() + orderDetail.getNumber());
                orders.setTotalPrice(orders.getTotalPrice() + orderDetail.getTotalPrice());
                ordersDao.save(orders);

                //减少该商品的总数量
                goods.setNumber(goods.getNumber() - number);
                goodsDao.save(goods);
                result = true;
            } else {
                logger.error("==== 订单项添加异常 ====");
            }
        } else {
            logger.error("==== 商品的数量不够 ====");
            throw new DomainSecurityException("添加商品" + goods.toString() + goods.getId()+ "失败");
        }
        logger.info("==== 退出addOrderDetailFromGoods方法 返回值类型:{ jBoolean:" + result + " } =======>");
        return result;
    }

    @Override
    public void deleteOrderDetail(Long ordersId) {
        logger.info("<====== 进入deleteOrderDetail:方法,{[orderId] :" + ordersId + " } ====");
        orderDetailDao.deleteByOrderId(Math.toIntExact(ordersId));
        logger.info("==== 退出deleteOrderDetail方法 返回值类型:{ void: } =======>");
    }
    @Override
    public List<OrderDetail> findOrdersDetail(Long ordersId) {
        logger.info("<====== 进入deleteOrderDetail:方法,{[orderId] :" + ordersId + " } ====");
        logger.info("==== 退出deleteOrderDetail方法 返回值类型:{ void: } =======>");
        return orderDetailDao.findByOrderId(Math.toIntExact(ordersId));
    }

    /**
     * Goods转换成OrderDetail
     * @param goods
     */
    private OrderDetail goodsToOrderDetail(Goods goods, Integer number) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setGoodName(goods.getName());
        orderDetail.setNumber(number);
        orderDetail.setPrice(goods.getPrice());
        orderDetail.setTotalPrice(goods.getPrice() * number);
        return orderDetail;
    }
}
